<template>
	<div class="flex-start-center">
		<el-popover v-model="showPopoverState" trigger="click" title="前纺计划" width="650">
			<!--<div class="margin-bottom-4">
				<el-button size="mini" icon="el-icon-plus" @click="onCreateFrontPlanBtn">新增</el-button>
			</div>-->
			<jk-table border :data="tableData" size="mini" :height="240">
				<vxe-column fixed="left" :width="140" align="left" field="code" title="单据编号">
					<template slot-scope="scope">
						<div class="flex-center">
							<el-button class="item-gap" size="mini" type="text" @click="onEditBtn(scope.row)">
								{{ scope.row.code }}
								<span class="el-icon-edit"></span>
							</el-button>
						</div>
					</template>
				</vxe-column>
				<vxe-column title="区域" align="left" field="workshopName" :width="90"></vxe-column>
				<vxe-column title="品种" align="left" field="productName" :min-width="160"></vxe-column>
				<vxe-column title="批号" align="left" field="batchCode" :min-width="140"></vxe-column>
				<vxe-column title="状态" align="center" field="openingState" :width="90">
					<template slot-scope="scope">
						<jk-state-bar :state="scope.row.auditState" :state-list="noticeStateList"></jk-state-bar>
					</template>
				</vxe-column>
			</jk-table>
			<div v-show="ruleForm.id" slot="reference">
				<el-button size="mini" icon="el-icon-tickets" class="">
					<span>前纺计划：</span>
					<span style="font-weight: 700; font-size: 13px;">{{ tableData.length }}</span>
					<span> 个</span>
				</el-button>
			</div>
		</el-popover>
		<el-button v-show="ruleForm.id && ruleForm.serviceState !== 3" size="mini" icon="el-icon-plus" @click="onCreateFrontPlanBtn"></el-button>
		<createFrontPlanDialog
			:data-id="null"
			:order-id="ruleForm.id"
			:dialog-state="createFrontPlanDialogState"
			@on-visible-change="onVisibleChangeOfCreateFrontPlanDialog"
			@on-finish="onFinishOfCreateFrontPlanDialog"
		></createFrontPlanDialog>
	</div>
</template>
<script>
    import { qslProductionPage } from '../../../../api/production/qslb';
    import { noticeStateList } from '../../../../utils/jk/status-option-list';
    import shareCategoryDialog from './share-category-dialog';
    import createFrontPlanDialog from './create-front-plan-dialog';
    import { tipsBar } from '../../../../utils/jk/jk-tool';

    export default {
        components: {
            createFrontPlanDialog
        },
        props: {
            ruleForm: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        data() {
            return {
                createFrontPlanDialogState: false,
                noticeStateList: noticeStateList,
                showPopoverState: false,
                tableData: [],
                orderId: null
            };
        },
        methods: {
            onFinishOfCreateFrontPlanDialog() {
                this.createFrontPlanDialogState = false;
                this.getListRequest();
            },
            onVisibleChangeOfCreateFrontPlanDialog(e) {
                this.createFrontPlanDialogState = e;
            },
            onCreateFrontPlanBtn() {
                const isSaved = this.ruleForm.productOrderBatchList.every(x => x.id);
                if (isSaved) {
                    this.orderId = this.ruleForm.id;
                    this.createFrontPlanDialogState = true;
                } else {
                    tipsBar(this, 'custom', 'warning', '订单批号信息未保存，请保存后再分配!');
                }
            },
            onEditBtn(row) {
                this.dataId = row.id;
                this.$toOtherRouter({
                    name: 'editForeSpinningPlan',
                    query: {
                        id: row.id
                    }
                });
            },
            getListRequest() {
                if (!this.ruleForm.code) return;
                return qslProductionPage({ productOrderCode: this.ruleForm.code }).then(res => {
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                    }
                });
            }
        },
        watch: {
            showPopoverState(newVal) {
                if (newVal) {
                    this.getListRequest();
                }
            }
        }
    };
</script>
